<template>
  <div class="container">
    <div class="user-profile-wrapper">
      <div class="user-profile" @click="toggleDropdown">
        <div class="avatar-container">
          <img 
            class="user-avatar" 
            src="../assets/images/header.jpg" 
            alt="用户头像"
          >
        </div>
        <span class="user-name">{{ userName }}</span>
        <i class="el-icon-arrow-down" :class="{ 'rotate': showDropdown }"></i>
        
        <!-- 下拉菜单 -->
        <transition name="el-zoom-in-top">
          <div v-show="showDropdown" class="dropdown-menu">
            <div class="dropdown-item" @click="goToProfile">
              <i class="el-icon-user"></i>
              <span>个人中心</span>
            </div>
            <div class="dropdown-item" @click="logout">
              <i class="el-icon-switch-button"></i>
              <span>退出登录</span>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userName: '张三',
      showDropdown: false
    };
  },
  methods: {
    toggleDropdown() {
      this.showDropdown = !this.showDropdown;
    },
    goToProfile() {
      this.$router.push('/main/person/basic');
      this.showDropdown = false;
    },
    logout() {
      this.$confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清除token或用户信息
        localStorage.removeItem('token');
        // 跳转到登录页
        this.$router.push('/login');
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        });
      });
    }
  },
  mounted() {
    // 点击页面其他区域关闭下拉菜单
    document.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.showDropdown = false;
      }
    });
  },
  beforeDestroy() {
    document.removeEventListener('click', this.closeDropdown);
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
  width: 100%;
}

.user-profile-wrapper {
  display: flex;
  justify-content: flex-end; 
  position: relative;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  position: relative;
  padding: 5px 10px;
  border-radius: 20px;
  transition: background-color 0.3s;
}

.user-profile:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.avatar-container {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid #e6e6e6;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-name {
  font-size: 16px;
  font-weight: 500;
  color: #fff;
}

.el-icon-arrow-down {
  color: #fff;
  transition: transform 0.3s;
  font-size: 14px;
}

.el-icon-arrow-down.rotate {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 60px;
  right: 0;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 10px 0;
  min-width: 160px;
  z-index: 100;
}

.dropdown-item {
  padding: 10px 20px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
}

.dropdown-item i {
  margin-right: 10px;
  color: #606266;
}

.dropdown-item span {
  color: #606266;
  font-size: 14px;
}
</style>